package com.shopping.service.corporatonRole;

import com.shopping.constant.ResponseCode;
import com.shopping.dao.CorporationRoleDao;
import com.shopping.pojo.CorporationRole;
import com.shopping.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CorporationRoleServiceImpl implements CorporationRoleService {

    @Autowired
    CorporationRoleDao corporationRoleDao;

    @Override
    public Map<String, Object> registerCorporation(CorporationRole corporationRole) {
        Map<String, Object> map = new HashMap<>();
        try {
            if(!CheckUtil.checkIdCard(corporationRole.getIdNumber())){
                map.put("code", ResponseCode.PARAM_ERROR.getValue());
                return map;
            }
            CorporationRole selectCor1 = corporationRoleDao.select(corporationRole.getId());
            if(selectCor1 != null){
                map.put("code", ResponseCode.ALREADY_HAVE_CORPORATION.getValue());
                return map;
            }
            CorporationRole selectCor2 = corporationRoleDao.selectByName(corporationRole.getCorporationName());
            if(selectCor2 != null){
                map.put("code", ResponseCode.ENTITY_ALREADY_EXISTS.getValue());
                return map;
            }
            corporationRoleDao.insert(corporationRole);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("corporationRole", corporationRole);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> scanOwnMessage(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            CorporationRole corporationRole = corporationRoleDao.select(id);
            if(corporationRole == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("corporationRole", corporationRole);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showCorporationMessage(int id) {
        Map<String, Object> map = new HashMap<>();
        try {
            CorporationRole corporationRole = corporationRoleDao.selectMessage(id);
            if(corporationRole == null){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("corporationRole", corporationRole);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> showCorporationMessageByName(String corporationName) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<CorporationRole> corporationRoles = corporationRoleDao.fuzzySelectByName(corporationName);
            if(corporationRoles.size() == 0){
                map.put("code", ResponseCode.ENTITY_NOT_EXISTS.getValue());
                return map;
            }
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("corporationRoles", corporationRoles);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }

    @Override
    public Map<String, Object> updateMessage(CorporationRole corporationRole) {
        Map<String, Object> map = new HashMap<>();
        try {
            corporationRoleDao.update(corporationRole);
            map.put("code", ResponseCode.REQUEST_SUCCESSED.getValue());
            map.put("corporationRole", corporationRole);
        }catch (Exception e){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            e.printStackTrace();
        }
        return map;
    }
}
